package com.yhj.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhj.reggie.dto.SetmealDto;
import com.yhj.reggie.entity.Category;
import com.yhj.reggie.entity.Page;
import com.yhj.reggie.entity.Setmeal;
import com.yhj.reggie.entity.SetmealDish;
import com.yhj.reggie.exception.BusinessException;
import com.yhj.reggie.mapper.CategoryMapper;
import com.yhj.reggie.mapper.SetmealDishMapper;
import com.yhj.reggie.mapper.SetmealMapper;
import com.yhj.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    /**
     * 添加套餐业务
     *
     * @param setmealDto
     */
    @Override
    public void save(SetmealDto setmealDto) {
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealMapper.save(setmealDto);

        List<SetmealDish> setmealDisheList = setmealDto.getSetmealDishes().stream().map((setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        })).collect(Collectors.toList());
        setmealDishMapper.save(setmealDisheList);
    }

    /**
     * 分页查询套餐数据,模糊查名
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findAll(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<Setmeal> setmealList = setmealMapper.findAll(name);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        List<SetmealDto> setmeaDtolList = setmealList.stream().map((setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;

        })).collect(Collectors.toList());
        Page<SetmealDto> objectPage = new Page<>();
        objectPage.setTotal(pageInfo.getTotal());
        objectPage.setRecords(setmeaDtolList);

        return objectPage;
    }


    /**
     * 批量删除套餐
     *
     * @param ids
     * @param updateUser
     * @return
     */
    @Override
    public boolean deleteById(List<Long> ids, Long updateUser) {
        Integer num = setmealMapper.countById(ids);
        if(num!=0){
            throw new BusinessException("含有启用的套餐");
        }
        setmealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());
        setmealDishMapper.deleteBySetmealId(ids,updateUser,LocalDateTime.now());

        return true;
    }
}
